using UnityEngine;
using QFramework;
using QFramework.Example;

namespace ProjectSurvivor
{
	public partial class GetAllExp : GameplayObject
	{
		void OnTriggerEnter2D(Collider2D other)
		{
			if (other.GetComponent<CollectableArea>())
			{
				foreach (var exp in FindObjectsByType<Exp>(FindObjectsInactive.Exclude, FindObjectsSortMode.None))
				{
					ActionKit.OnUpdate.Register(() =>
					{
						var player = Player.Default;
						if (player)
						{
							var direction = player.Position() - exp.Position();
							exp.transform.Translate(direction.normalized * Time.deltaTime * 5f);
						}
					}).UnRegisterWhenGameObjectDestroyed(exp);
				}
				foreach (var coin in FindObjectsByType<Coin>(FindObjectsInactive.Exclude, FindObjectsSortMode.None))
				{
					ActionKit.OnUpdate.Register(() =>
					{
						var player = Player.Default;
						if (player)
						{
							var direction = player.Position() - coin.Position();
							coin.transform.Translate(direction.normalized * Time.deltaTime * 5f);
						}
					}).UnRegisterWhenGameObjectDestroyed(coin);
				}
				AudioKit.PlaySound("GetAllExp");
				//删除
				this.DestroyGameObjGracefully();
			}

		}
		protected override Collider2D Collider2D => SelfCollider2D;
	}
}
